from django.conf import settings
from itsdangerous import TimedJSONWebSignatureSerializer as TJWSSerializer

# 生成加密的url链接地址
from users.models import User


def generate_verify(user):
    # 创建数据信息
    dict_data = {"user_id": user.id, "email": user.email}
    # 生成加密对象
    serializer = TJWSSerializer(secret_key=settings.SECRET_KEY, expires_in=1800)
    # 加密
    token = serializer.dumps(dict_data)
    # 拼接链接
    verify_url = "%s?token=%s" % (settings.EMAIL_VERIFY_URL, token.decode())
    # 返回
    return verify_url


# 解密加密的token
def decode_token(token):
    # 1,创建TJSWSerializer对象
    serializer = TJWSSerializer(secret_key=settings.SECRET_KEY, expires_in=60 * 30)

    # 2,解密数据
    try:
        dict_data = serializer.loads(token)

        user_id = dict_data.get("user_id")

        user = User.objects.get(id=user_id)

    except Exception:
        return None

    # 3,返回user
    return user
